<template slot="title">
  <el-dialog :title="editTab?'编辑规则':'新增规则'" :close-on-click-modal="false" :visible.sync="visible" width="800px">
    <el-container>
      <el-main style="max-height:60vh">
        <el-form :model="dataForm" ref="dataForm"  @keyup.enter.native="dataFormSubmit()" label-width="140px">
          <el-form-item label="规则名称" prop="gzName" :rules="[
          { required: true, message: '规则名称不能为空', trigger: 'blur' },
          { min:1, max:200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ]">
            <el-input v-model="dataForm.gzName" placeholder="规则名称" style="width: 90%"></el-input>
          </el-form-item>

          <el-form-item label="应用范围" prop="yyFw" :rules="[
            { required: true, message: '应用范围不能为空', trigger: 'blur' }
          ]">
            <el-select v-model="dataForm.yyFw" style="width: 90%">
              <el-option v-for="(item,index) in ['个性','通用']" :key="index" :label="item" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">

      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  /**
   * 绩效考核_测评_评分规则表单
   * @author zgc
   * @date 2021-06-16 10:06:44
   */
  import {
    getObj,
    addObj,
    putObj
  } from '@/api/ceping/cepinggzxxb';
  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        editTab: false,
        dataForm: {
          gzName:'',
          yyFw:''
        },
        //  dataRule: {
        //    gzName:[],
        //    yyFw:[]
        //  }
      };
    },
    methods: {
      init(id) {
        this.visible = true;
        this.canSubmit = true
        this.editTab = false;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (id) {
            this.dataForm.dataList = [];
            getObj(id).then(response => {
              this.dataForm = response.data.data;
              this.editTab = true;
            });
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.editTab) {
              putObj(this.dataForm).then(data => {
                this.$notify.closeAll();
                this.$notify.success('修改成功');
                this.visible = false;
                this.$emit('refreshDataList');
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.closeAll();
                this.$notify.success('添加成功');
                this.visible = false;
                this.$emit('refreshDataList');
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        });
      },
    }
  };

</script>
